package com.warski.wonderfires.page.firedance;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.warski.wonderfires.EspTouchActivity;
import com.warski.wonderfires.R;
import com.warski.wonderfires.TheApplication;


/**
 * 初始页面
 */
public class DanceFragment extends Fragment {

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (TheApplication.isConnection) {
            Log.d("DanceFragment","onCreateView action_DanceFragment_to_FireDanceFragment");
            NavHostFragment.findNavController(DanceFragment.this).navigate(R.id.action_DanceFragment_to_FireDanceFragment);
        }
        View root = inflater.inflate(R.layout.fragment_dance, container, false);
        initView(root);

        return root;
    }

    private void initView(View root) {
        root.findViewById(R.id.titleLeft).setOnClickListener(v -> goToBle());

        ImageView appName = root.findViewById(R.id.app_name);
        appName.setImageResource(R.mipmap.app_name);

        ImageView fireImage = root.findViewById(R.id.fire_image);
        fireImage.setImageResource(R.mipmap.no_fire);

        ImageView minFireImage = root.findViewById(R.id.min_fire_image);
        minFireImage.setImageResource(R.mipmap.min_no_fire);

        ImageView maxFireImage = root.findViewById(R.id.max_fire_image);
        maxFireImage.setImageResource(R.mipmap.min_no_fire);

        TextView ipText = root.findViewById(R.id.ip_text);
        ipText.setTextColor(Color.WHITE);
        ipText.setText(R.string.unconnected_device);

    }

    /**跳转蓝牙配网界面*/
    private void goToBle() {
        Intent intent = new Intent(getContext(), EspTouchActivity.class);
        startActivityForResult(intent, 200);
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.d("TAG","run onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 200) {
            // 如果网络连接则切换连接成功界面
            if (TheApplication.isConnection){
                NavHostFragment.findNavController(DanceFragment.this).navigate(R.id.action_DanceFragment_to_FireDanceFragment);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
